<template>
  <div class="userManagePage">
    <!-- 搜索栏 -->
    <a-form layout="inline" :model="searchParam" @finish="doSearch">
      <a-form-item label="账号">
        <a-input v-model:value="searchParam.userAccount" placeholder="输入账号" />
      </a-form-item>
      <a-form-item label="用户名">
        <a-input v-model:value="searchParam.userName" placeholder="输入用户名" />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
    </a-form>
    <div style="margin-bottom: 16px"></div>
    <!-- 表格 -->
    <a-table
      :columns="columns"
      :data-source="dataList"
      :pagination="pagination"
      @change="doTableChange"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'userAvatar'">
          <a-image :src="record.userAvatar" :width="50" />
        </template>
        <template v-else-if="column.dataIndex === 'userRole'">
          <div v-if="record.userRole === 'admin'">
            <a-tag color="green">管理员</a-tag>
          </div>
          <div v-else>
            <a-tag color="blue">用户</a-tag>
          </div>
        </template>
        <template v-else-if="column.dataIndex === 'createTime'">
          {{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-else-if="column.dataIndex === 'updateTime'">
          {{ dayjs(record.updateTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-else-if="column.key === 'action'">
          <a-button type="primary" danger @click="handleDelete(record.id)">删除</a-button>
        </template>
      </template>
    </a-table>
  </div>
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted, computed } from 'vue'
import { deleteUserUsingPost, listUserByPageUsingPost } from '@/api/user'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
import { disposePinia } from 'pinia'
const columns = [
  {
    title: 'id',
    dataIndex: 'id',
  },
  {
    title: '账号',
    dataIndex: 'userAccount',
  },
  {
    title: '用户名',
    dataIndex: 'userName',
  },
  {
    title: '用户头像',
    dataIndex: 'userAvatar',
  },
  {
    title: '用户简介',
    dataIndex: 'userProfile',
  },
  {
    title: '用户角色',
    dataIndex: 'userRole',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },

  {
    title: '操作',
    key: 'action',
  },
]

const dataList = ref<API.UserVO[]>([])
const total = ref(0)
//搜索条件
const searchParam = reactive<API.UserQueryRequest>({
  pageNum: 1,
  pageSize: 10,
  sortField: '',
  sortOrder: '',
  userName: '',
  userRole: '',
  userAccount: '',
})

const pagination = computed(() => {
  return {
    pageNum: searchParam.pageNum,
    pageSize: searchParam.pageSize,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total: number) => `共 ${total} 条数据`,
  }
})
async function fetchData() {
  const res = await listUserByPageUsingPost({ ...searchParam })
  if (res.data.code == 200 && res.data.data) {
    dataList.value = res.data.data.records ?? []
    console.log(dataList)

    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取数据失败' + res.data.message)
  }
}

//表格变换后,重新获取数据
function doTableChange(pagination: any) {
  searchParam.pageNum = pagination.current
  searchParam.pageSize = pagination.pageSize
  fetchData()
}
//搜索逻辑
function doSearch() {
  searchParam.pageNum = 1
  searchParam.pageSize = 10
  fetchData()
}
//删除逻辑
function handleDelete(id: number) {
  if (!id) {
    return
  }
  deleteUserUsingPost({ id })
    .then((res) => {
      if (res.data.code == 200) {
        fetchData()
        message.success('删除成功')
      } else {
        message.success('删除成功')
      }
    })
    .catch((err) => {
      message.error('删除失败' + err.data.message)
    })
}

//页面加载时获取数据
onMounted(() => {
  fetchData()
})
</script>

